import 'package:easy_refresh/easy_refresh.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/model/mine/profit_dynamic_item_model.dart';

import '../../../../components/base_refresh/base_refresh_page_counter.dart';
import '../../../../http/api/api.dart';

class ProfitBrushVideoListController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final _pageSize = 50;
  List<ProfitDynamicItemModel> list = List<ProfitDynamicItemModel>.of([]);

  @override
  void onInit() {
    onRefresh();
    super.onInit();
  }

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    final resp = await Api.fecthSpreadDynamiProfitList(
        page: page, pageSize: _pageSize, incomeType: 3);
    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    final resp = await Api.fecthSpreadDynamiProfitList(
        page: page, pageSize: _pageSize, incomeType: 3);
    if (resp == null) {
      return IndicatorResult.fail;
    }

    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }

    incPage();
    return IndicatorResult.success;
  }

  void _setList(List<ProfitDynamicItemModel>? items) {
    list.clear();
    _addList(items);
  }

  void _addList(List<ProfitDynamicItemModel>? items) {
    if (null != items && items.isNotEmpty) {
      list.addAll(items);
      update();
    }
  }
}
